home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / FontDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.1 KB  |  154 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This is like the ShapesDemo applet, except that it handles fonts more
  22.  * carefully.
  23.  */
  24.  
  25. public class FontDemo extends Applet {
  26.     final static int maxCharHeight = 15;
  27.  
  28.     public void init() {
  29.         validate();
  30.     }
  31.  
  32.     public void paint(Graphics g) {
  33.         Dimension d = size();
  34.         int x = 5;
  35.         int y = 7;
  36.  
  37.         Color bg = getBackground();
  38.         Color fg = getForeground();
  39.  
  40.         int gridWidth = d.width / 7;
  41.         int gridHeight = d.height / 2;
  42.         int stringY;
  43.         int rectWidth = gridWidth - 2*x;
  44.         int rectHeight; 
  45.  
  46.         boolean fontFits = false;
  47.         Font font = g.getFont();
  48.         FontMetrics fontMetrics = g.getFontMetrics();
  49.         while (!fontFits) {
  50.             if ((fontMetrics.getHeight() <= maxCharHeight) 
  51.                 && (fontMetrics.stringWidth("drawRoundRect()") 
  52.                     <= gridWidth)) {
  53.                 fontFits = true;
  54.             } else {
  55.             //At first, accidentally left out "g.", which worked
  56.             //but fontMetrics never changed.
  57.                 g.setFont(font = new Font(font.getName(),
  58.                                             font.getStyle(),
  59.                                         font.getSize() - 1));
  60.                 fontMetrics = g.getFontMetrics();
  61.             }
  62.         }
  63.  
  64.         stringY = gridHeight - 5 - fontMetrics.getDescent();
  65.         rectHeight = stringY - fontMetrics.getMaxAscent() - y - 2;
  66.  
  67.         g.setColor(bg);
  68.         g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
  69.         g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
  70.         g.setColor(fg);
  71.  
  72.         // drawLine() 
  73.         g.drawLine(x, y+rectHeight-1, x + rectWidth, y); // x1, y1, x2, y2
  74.         g.drawString("drawLine()", x, stringY);
  75.         x += gridWidth;
  76.  
  77.         // drawRect() 
  78.         g.drawRect(x, y, rectWidth, rectHeight); // x, y, width, height
  79.         g.drawString("drawRect()", x, stringY);
  80.         x += gridWidth;
  81.  
  82.         // draw3DRect() 
  83.         g.setColor(bg);
  84.         g.draw3DRect(x, y, rectWidth, rectHeight, true);
  85.         g.setColor(fg);
  86.         g.drawString("draw3DRect()", x, stringY);
  87.         x += gridWidth;
  88.  
  89.         // drawRoundRect() 
  90.         g.drawRoundRect(x, y, rectWidth, rectHeight, 10, 10); // x, y, w, h, arcw, arch
  91.         g.drawString("drawRoundRect()", x, stringY);
  92.         x += gridWidth;
  93.  
  94.         // drawOval() 
  95.         g.drawOval(x, y, rectWidth, rectHeight); // x, y, w, h
  96.         g.drawString("drawOval()", x, stringY);
  97.         x += gridWidth;
  98.  
  99.         // drawArc() 
  100.         g.drawArc(x, y, rectWidth, rectHeight, 90, 135); // x, y, w, h
  101.         g.drawString("drawArc()", x, stringY);
  102.         x += gridWidth;
  103.  
  104.         // drawPolygon() 
  105.         Polygon polygon = new Polygon();
  106.         polygon.addPoint(x, y);
  107.         polygon.addPoint(x+rectWidth, y+rectHeight);
  108.         polygon.addPoint(x, y + rectHeight);
  109.         polygon.addPoint(x + rectWidth, y);
  110.         g.drawPolygon(polygon); 
  111.         g.drawString("drawPolygon()", x, stringY);
  112.  
  113.         x = 5 + gridWidth;
  114.         y += gridHeight;
  115.         stringY += gridHeight;
  116.  
  117.         // fillRect() 
  118.         g.fillRect(x, y, rectWidth, rectHeight); // x, y, width, height
  119.         g.drawString("fillRect()", x, stringY);
  120.         x += gridWidth;
  121.  
  122.         // fill3DRect() 
  123.         g.setColor(bg);
  124.         g.fill3DRect(x, y, rectWidth, rectHeight, true);
  125.         g.setColor(fg);
  126.         g.drawString("fill3DRect()", x, stringY);
  127.         x += gridWidth;
  128.  
  129.         // fillRoundRect() 
  130.         g.fillRoundRect(x, y, rectWidth, rectHeight, 10, 10); // x, y, w, h, arcw, arch
  131.         g.drawString("fillRoundRect()", x, stringY);
  132.         x += gridWidth;
  133.  
  134.         // fillOval() 
  135.         g.fillOval(x, y, rectWidth, rectHeight); // x, y, w, h
  136.         g.drawString("fillOval()", x, stringY);
  137.         x += gridWidth;
  138.  
  139.         // fillArc() 
  140.         g.fillArc(x, y, rectWidth, rectHeight, 90, 135); // x, y, w, h
  141.         g.drawString("fillArc()", x, stringY);
  142.         x += gridWidth;
  143.  
  144.         // fillPolygon() 
  145.         Polygon filledPolygon = new Polygon();
  146.         filledPolygon.addPoint(x, y);
  147.         filledPolygon.addPoint(x+rectWidth, y+rectHeight);
  148.         filledPolygon.addPoint(x, y+rectHeight);
  149.         filledPolygon.addPoint(x+rectWidth, y);
  150.         g.fillPolygon(filledPolygon); 
  151.         g.drawString("fillPolygon()", x, stringY);
  152.     }
  153. }
  154.